package org.footito.android.activity;

import java.io.IOException;

import org.footito.android.data.TeamStatus;
import org.footito.android.data.update.UpdateListener;
import org.footito.android.data.update.UpdateResults;
import org.footito.android.data.update.UpdaterTools;

import android.content.Context;
import android.os.AsyncTask;

/**
 * FIXME need to be removed
 * 
 * @author 15pouces
 * 
 */
public class UpdateTask extends AsyncTask<Void, Integer, Void> implements
		UpdateListener {
	private Exception error = null;
	private Context context;
	private UpdateListener listener;
	private UpdateResults results;

	public UpdateTask(Context context, UpdateListener listener) {
		this.context = context;
		this.listener = listener;
	}

	@Override
	public void onComplete(UpdateResults results) {
		this.results = results;
	}

	@Override
	public void onError(Exception e) {
		if (e instanceof IOException)
			error = e;
	};

	@Override
	protected void onProgressUpdate(Integer... progressId) {
		listener.onProgress(progressId);
	}

	@Override
	public void onProgress(Integer... progressId) {
		publishProgress(progressId);
	}

	@Override
	protected Void doInBackground(Void... params) {
		UpdaterTools.updatingCompleteProcess(context, this);
		return null;
	}

	protected void onPostExecute(Void result) {
		if (error != null) {
			listener.onError(error);
		} else {
			listener.onComplete(results);
		}
	}

}